package com.youlin.security;

import com.youlin.entities.BygAdmin;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

public class SecurityUser implements UserDetails {

    public SecurityUser(BygAdmin bygAdmin) {
        this.bygAdmin = bygAdmin;
    }

    public SecurityUser(BygAdmin bygAdmin, String appName) {
        this.bygAdmin = bygAdmin;
        this.appName = appName;
    }

    public BygAdmin bygAdmin;

    public String appName;

    public Integer getUserId () {
        return bygAdmin.getId();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Arrays.asList(new SimpleGrantedAuthority(bygAdmin.getRole().getName()));
    }

    @Override
    public String getPassword() {
        return bygAdmin.getPwd();
    }

    @Override
    public String getUsername() {
        return bygAdmin.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return bygAdmin.getUsable();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SecurityUser that = (SecurityUser) o;
        return Objects.equals(bygAdmin.getId(), that.bygAdmin.getId());
    }

    @Override
    public int hashCode() {
        return Objects.hash(bygAdmin.getId());
    }

}
